package com.joesem.sale.client.logic.workOrder

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import com.jiaxing.sale.client.databinding.ActivityOrderAssignmentBinding
import com.jiaxing.sale.client.databinding.ItemCollaborationBinding
import com.jiaxing.sale.client.databinding.LayoutToolbarBinding
import com.joesem.sale.client.base.*
import com.joesem.sale.client.logic.workOrder.AssignmentResult.result
import com.joesem.sale.client.logic.workOrder.viewModel.OrderCreateViewModel
import com.joesem.sale.client.network.data.DeptUserMode
import com.joesem.sale.client.network.data.OrderDetailsMode
import com.joesem.sale.client.utils.click
import com.joesem.sale.client.widget.dialog.optionDeptUser
import com.joesem.sale.client.widget.dialog.optionMultipleDeptUser


internal object AssignmentResult {
    var result: (OrderDetailsMode) -> Unit = {}
}

class OrderAssignmentActivity : BaseActivity<ActivityOrderAssignmentBinding>() {

    private val viewModel by createViewModel<OrderCreateViewModel>()

    private val values = hashMapOf<String, Any?>()
    private val selectedCollaboration = arrayListOf<DeptUserMode>()
    override fun LayoutToolbarBinding.toolbar(savedInstanceState: Bundle?) {
        visible()
        toolbarTheme()
        toolbarTitle.text = "工单指派"
    }

    override fun ActivityOrderAssignmentBinding.initView(savedInstanceState: Bundle?) {

    }

    override fun ActivityOrderAssignmentBinding.initListener() {
        btnResponsibility.click {
            optionDeptUser(this@OrderAssignmentActivity, "选择工单负责人") {
                ivAdd1.gone()
                layoutResponsibility.visible()
                layoutResponsibility.tvName.text = it.realname.orEmpty()
                values["user_id"] = it.id
            }
        }
        btnCollaboration.click {
            optionMultipleDeptUser(this@OrderAssignmentActivity, "选择工单协作人", selectedCollaboration) {
                selectedCollaboration.clear()
                selectedCollaboration.addAll(it)
                addCollaborationItem(selectedCollaboration)
            }
        }
        btnSubmit.click {
            if (values["user_id"] == null) {
                toast("请选择工单负责人")
                return@click
            }
            viewModel.orderAllot(intent.getIntExtra("orderId", -1), values)
        }
    }

    override fun ActivityOrderAssignmentBinding.initIntent(intent: Any?) {
        when (intent) {
            is OrderCreateViewModel.OrderAllotIntent -> {
                result.invoke(intent.result)
                finish()
            }
        }
    }

    private fun addCollaborationItem(data: ArrayList<DeptUserMode>) {
        values["assist_user_ids"] = data.map { it.id }
        bind.ivAdd2.gone()
        bind.cgCollaboration.removeAllViews()
        data.forEach { mode ->
            val itemBind = ItemCollaborationBinding.inflate(LayoutInflater.from(this))
            itemBind.tvName.text = mode.realname.orEmpty()
            itemBind.root.click {
                bind.cgCollaboration.removeView(itemBind.root)
                data.removeAll { it.id == mode.id }
                values["assist_user_ids"] = data.map { it.id }
                if (data.isEmpty()) {
                    bind.ivAdd2.visible()
                }
            }
            bind.cgCollaboration.addView(itemBind.root)
        }
    }
}

fun optionOrderAssignment(context: Context, id: Int?, result: (OrderDetailsMode) -> Unit) {
    AssignmentResult.result = result
    context.launch<OrderAssignmentActivity>("orderId" to id)
}